package com.example.demo.client.view;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyHashMap;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.*;
import com.example.demo.server.controller.ArticleController;
import com.example.demo.server.model.ArticleInfo;
import com.example.demo.server.model.UserInfo;


public class ListView implements View{

    private ArticleController articleController = new ArticleController();

    @Override
    public CommonRequest jump(CommonRequest request) {
        MyHashMap<String, Object> map = (MyHashMap<String, Object>) request.getBody();
        if(map == null) {
            return showPage(1, 2);
        }
        Integer pindex = (Integer) map.get("pindex");
        if(pindex == null) {
            pindex = 1;
        }
        Integer psize = (Integer) map.get("psize");
        if(psize == null) {
            psize = 2;
        }
        return showPage(pindex, psize);

    }

    /**
     *  展示页面
     */
    public CommonRequest showPage(int pindex, int psize) {
        UserInfo userInfo = SessionUtils.getUser();
        CommonResponse response = articleController.allList(pindex, psize);
        if(userInfo == null) {
            return showPageNoLogin(pindex, psize, (MyHashMap<String, Object>) response.getData());
        }else {
            return showPageLogin(userInfo, pindex, psize, (MyHashMap<String, Object>) response.getData());
        }
    }



    public CommonRequest showPageLogin(UserInfo userInfo, int pindex, int psize, MyHashMap<String, Object> data) {
        MyList<ArticleInfo> list = (MyList<ArticleInfo>) data.get("list");
        int pMax = (int) data.get("pMax");
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.printf(" %s [%s]      我的博客（mylist）  写博客（write）  退出登录（logout）         \n", APPUtils.now(), userInfo.getName());
        System.out.println("+--------------------------------------------------------------------------------+");
        if(list.size() == 0) {
            System.out.println("|                                                                                |");
            System.out.println("| Nothing...                                                                     |");
            System.out.println("|                                                                                |");
        }
        for (int i = 0; i < list.size(); i++) {
            ArticleUtils.printBriefLowBlog(list.get(i), i + 1);
        }
        System.out.println("|________________________________________________________________________________|");
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        if(list.size() == 0) {
            System.out.println("|       [first]  [prev]  [next]  [last]  ( now: 0 / max: 0)                      |");
        }else {
            System.out.printf("|       [first]  [prev]  [next]  [last]  ( now:%02d / max:%02d)                      |\n", pindex, pMax);
        }
        System.out.println("+--------------------------------------------------------------------------------+");

        String command = APPUtils.input();
        MyList<String> handledCommand = APPUtils.handleCommand(command);
        if(handledCommand.size() == 2 && handledCommand.get(1).matches("^[1-9]\\d*$")) {
            int index = Integer.parseInt(handledCommand.get(1));
            //命令错误
            if(index > list.size()) {
                APPUtils.printPopUp("ERROR: Please Again");
                return CommonRequest.createRequest(ViewKey.LIST);
            }
            //命令成功
            String opera = handledCommand.get(0);
            switch (opera) {
                case ViewKey.DETAIL:
                    return CommonRequest.createRequest(ViewKey.DETAIL, list.get(index - 1));
                default:
                    APPUtils.printPopUp("ERROR: Please Again");
                    return CommonRequest.createRequest(ViewKey.LIST);
            }
        } else if(handledCommand.size() == 2 && handledCommand.get(0).equals(ViewKey.LIST)
                                    && !handledCommand.get(1).matches("^[1-9]\\d*$")) {
            String opera = handledCommand.get(1);
            MyHashMap<String, Object> map = new MyHashMap<>();
            switch (opera) {
                case "first":
                    if(pindex <= 1) {
                        APPUtils.printPopUp("已在首页");
                        return CommonRequest.createRequest(ViewKey.LIST);
                    }
                    map.put("pindex", 1);
                    map.put("psize", psize);
                    break;
                case "prev":
                    if(pindex <= 1) {
                        APPUtils.printPopUp("已在首页");
                        return CommonRequest.createRequest(ViewKey.LIST);
                    }
                    map.put("pindex", pindex - 1);
                    map.put("psize", psize);
                    break;
                case "next":
                    if(pindex >= pMax) {
                        APPUtils.printPopUp("已在末页");
                        map.put("pindex", pMax);
                    }else {
                        map.put("pindex", pindex + 1);
                    }
                    map.put("psize", psize);
                    break;
                case "last":
                    if(pindex >= pMax) {
                        APPUtils.printPopUp("已在末页");
                    }
                    map.put("pindex", pMax);
                    map.put("psize", psize);
                    break;
                default:
                    APPUtils.printPopUp("ERROR: Please Again");
                    return CommonRequest.createRequest(ViewKey.LIST);
            }
            return CommonRequest.createRequest(ViewKey.LIST, map);
        }else if(handledCommand.size() == 1) {
            String view = handledCommand.get(0);
            CommonRequest commonRequest = APPUtils.checkJump(view,
                    ViewKey.LOGOUT, ViewKey.MY_LIST, ViewKey.LIST, ViewKey.WRITE);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again");
        return CommonRequest.createRequest(ViewKey.LIST);
    }
    public CommonRequest showPageNoLogin(int pindex, int psize, MyHashMap<String, Object> data) {
        MyList<ArticleInfo> list = (MyList<ArticleInfo>) data.get(ViewKey.LIST);
        int pMax = (int) data.get("pMax");
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.println("| [未登录]                                                        登录（login）     |");
        System.out.println("+--------------------------------------------------------------------------------+");
        if(list.size() == 0) {
            System.out.println("|                                                                                |");
            System.out.println("| Nothing...                                                                     |");
            System.out.println("|                                                                                |");
        }
        for (int i = 0; i < list.size(); i++) {
            ArticleUtils.printBriefLowBlog(list.get(i), i + 1);
        }
        System.out.println("|________________________________________________________________________________|");
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        if(list.size() == 0) {
            System.out.println("|       [first]  [prev]  [next]  [last]  ( now: 0 / max: 0)                      |");
        }else {
            System.out.printf("|       [first]  [prev]  [next]  [last]  ( now:%02d / max:%02d)                      |\n", pindex, pMax);
        }
        System.out.println("+--------------------------------------------------------------------------------+");

        String command = APPUtils.input();
        MyList<String> handledCommand = APPUtils.handleCommand(command);
        if(handledCommand.size() == 2 && handledCommand.get(1).matches("^[1-9]\\d*$")) {
            int index = Integer.parseInt(handledCommand.get(1));
            //命令错误
            if(index > list.size()) {
                APPUtils.printPopUp("ERROR: Please Again");
                return CommonRequest.createRequest(ViewKey.LIST);
            }
            //命令成功
            String opera = handledCommand.get(0);
            switch (opera) {
                case ViewKey.DETAIL:
                    return CommonRequest.createRequest(ViewKey.DETAIL, list.get(index - 1));
                default:
                    APPUtils.printPopUp("ERROR: Please Again");
                    return CommonRequest.createRequest(ViewKey.LIST);
            }
        } else if(handledCommand.size() == 2 && handledCommand.get(0).equals(ViewKey.LIST)
                                        && !handledCommand.get(1).matches("^[1-9]\\d*$")) {
            String opera = handledCommand.get(1);
            MyHashMap<String, Object> map = new MyHashMap<>();
            switch (opera) {
                case "first":
                    if(pindex <= 1) {
                        APPUtils.printPopUp("已在首页");
                        return CommonRequest.createRequest(ViewKey.LIST);
                    }
                    map.put("pindex", 1);
                    map.put("psize", psize);
                    break;
                case "prev":
                    if(pindex <= 1) {
                        APPUtils.printPopUp("已在首页");
                        return CommonRequest.createRequest(ViewKey.LIST);
                    }
                    map.put("pindex", pindex - 1);
                    map.put("psize", psize);
                    break;
                case "next":
                    if(pindex >= pMax) {
                        APPUtils.printPopUp("已在末页");
                        map.put("pindex", pMax);
                    }else {
                        map.put("pindex", pindex + 1);
                    }
                    map.put("psize", psize);
                    break;
                case "last":
                    if(pindex >= pMax) {
                        APPUtils.printPopUp("已在末页");
                    }
                    map.put("pindex", pMax);
                    map.put("psize", psize);
                    break;
                default:
                    APPUtils.printPopUp("ERROR: Please Again");
                    return CommonRequest.createRequest(ViewKey.LIST);
            }
            return CommonRequest.createRequest(ViewKey.LIST, map);
        }else if(handledCommand.size() == 1) {
            String view = handledCommand.get(0);
            CommonRequest commonRequest = APPUtils.checkJump(view, ViewKey.LOGIN, ViewKey.LIST);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again");
        return CommonRequest.createRequest(ViewKey.LIST);
    }
}
